package com.lonwin.inspectionmanagement.mvp.model;

import android.app.Application;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.jess.arms.integration.AppManager;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import com.jess.arms.di.scope.ActivityScope;

import javax.inject.Inject;

import com.jess.arms.utils.DataHelper;
import com.lonwin.inspectionmanagement.app.AppConstants;
import com.lonwin.inspectionmanagement.app.utils.DataUtil;
import com.lonwin.inspectionmanagement.mvp.contract.CreatPointRecordContract;
import com.lonwin.inspectionmanagement.mvp.model.api.service.BusinessService;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.BaseBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ConfigBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.CreaQuestResultBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.PointHistoryBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ResQdetailBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.SaveResultBean;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.functions.Function;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;


@ActivityScope
public class CreatPointRecordModel extends BaseModel implements CreatPointRecordContract.Model {
    @Inject
    Gson mGson;
    @Inject
    Application mApplication;
    private BusinessService mBusinessService;
    @Inject
    public CreatPointRecordModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
        this.mBusinessService=repositoryManager.obtainRetrofitService(BusinessService.class);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }

    @Override
    public Observable<String> uploadFile(String url,File file) {

        RequestBody requestFile =
                RequestBody.create(MediaType.parse("application/otcet-stream"), file);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("file", file.getName(), requestFile);

        return mBusinessService.uploadFile(url,body);
    }

    @Override
    public Observable<List<ConfigBean.XcglMenuBean>> getListProblems() {
           return Observable.just(((ConfigBean)DataHelper.getDeviceData(AppManager.getAppManager().getCurrentActivity(),AppConstants.EXTRA_CONFIG_DATA)).getXcglMenu());

    }

    @Override
    public Observable<SaveResultBean> savePointData(String url, Map<String, String> maps) {
        return mBusinessService.savePoint(url,maps).map(new Function<SaveResultBean, SaveResultBean>() {
            @Override
            public SaveResultBean apply(SaveResultBean saveResultBean) throws Exception {
                return saveResultBean;
            }
        });
    }

    @Override
    public Observable<PointHistoryBean.DataBean> editPoint(String url, Map<String, String> maps) {
        return mBusinessService.editPoint(url,maps).map(new Function<PointHistoryBean.DataBean, PointHistoryBean.DataBean>() {
            @Override
            public PointHistoryBean.DataBean apply(PointHistoryBean.DataBean dataBean) throws Exception {
                return dataBean;
            }
        });
    }

    @Override
    public Observable<CreaQuestResultBean> addQuestTemp(String url, Map<String, Object> maps) {
        return mRepositoryManager.obtainRetrofitService(BusinessService.class).addQuestTemp(url,maps)
                .map(new Function<CreaQuestResultBean, CreaQuestResultBean>() {
                    @Override
                    public CreaQuestResultBean apply(CreaQuestResultBean creaQuestResultBean) throws Exception {
                        return creaQuestResultBean;
                    }
                });
    }

    @Override
    public Observable<ResQdetailBean> loadDetail(String url , String id) {
        HashMap<String,String> params=new HashMap();
        params.put("questid",id);
        return mRepositoryManager.obtainRetrofitService(BusinessService.class).getMethodData(url,params).map(new Function<String, ResQdetailBean>() {
            @Override
            public ResQdetailBean apply(String s) throws Exception {
                return mGson.fromJson(s,ResQdetailBean.class);
            }
        });
    }


    @Override
    public Observable<ResQdetailBean> updateQuestion(String url,ResQdetailBean.DataBean resQdetailBean) {
        HashMap<String,String> params=new HashMap<>();
        if (!TextUtils.isEmpty(resQdetailBean.getAdcd())){
            params.put("adcd",resQdetailBean.getAdcd());
        }
        if (!TextUtils.isEmpty(resQdetailBean.getAdcdnm())){
            params.put("adcdnm",resQdetailBean.getAdcdnm());
        }
        if (!TextUtils.isEmpty(resQdetailBean.getAddr())){
            params.put("addr",resQdetailBean.getAddr());
        }
        if (!TextUtils.isEmpty(resQdetailBean.getAudio())){
            params.put("audio",resQdetailBean.getAudio());
        }
        if (!TextUtils.isEmpty(resQdetailBean.getAudiotm())){
            params.put("audiotm",resQdetailBean.getAudiotm());
        }
        if (!TextUtils.isEmpty(resQdetailBean.getContent())){
            params.put("content",resQdetailBean.getContent());
        }
        if (!TextUtils.isEmpty(resQdetailBean.getGpsid())){
            params.put("gpsid",resQdetailBean.getGpsid());
        }
        if (!TextUtils.isEmpty(resQdetailBean.getGroupnm())){
            params.put("groupid",resQdetailBean.getGroupid());
        }
        if (!TextUtils.isEmpty(resQdetailBean.getGroupnm())){
            params.put("groupnm",resQdetailBean.getGroupnm());
        }
        if (!TextUtils.isEmpty(resQdetailBean.getGpsnm())){
            params.put("gpsnm",resQdetailBean.getGpsnm());
        }
        params.put("img1",TextUtils.isEmpty(resQdetailBean.getImg1())?"":resQdetailBean.getImg1());
        params.put("img2",TextUtils.isEmpty(resQdetailBean.getImg2())?"":resQdetailBean.getImg2());
        params.put("img3",TextUtils.isEmpty(resQdetailBean.getImg3())?"":resQdetailBean.getImg3());
        params.put("img4",TextUtils.isEmpty(resQdetailBean.getImg4())?"":resQdetailBean.getImg4());
        if (!TextUtils.isEmpty(resQdetailBean.getQuestid())){
            params.put("questid",resQdetailBean.getQuestid());
        }
        if (resQdetailBean.getQusetTypeTempRel()!=null){
            params.put("qusetTypeTempRel",mGson.toJson(resQdetailBean.getQusetTypeTempRel()));
        }
        if (resQdetailBean.getQusetAttrTempRel()!=null){
            params.put("qusetAttrTempRel",mGson.toJson(resQdetailBean.getQusetAttrTempRel()));
        }
        params.put("x",resQdetailBean.getX()+"");
        params.put("y",resQdetailBean.getY()+"");
        return mRepositoryManager.obtainRetrofitService(BusinessService.class).postMethodData(url,params).map(new Function<String, ResQdetailBean>() {
            @Override
            public ResQdetailBean apply(String s) throws Exception {
                return mGson.fromJson(s,ResQdetailBean.class);
            }
        });
    }

    @Override
    public Observable<BaseBean> deleteQuestion(String url, String id) {
        HashMap<String,String> params=new HashMap();
        params.put("questid",id);
        return mRepositoryManager.obtainRetrofitService(BusinessService.class).getMethodData(url,params).map(new Function<String, BaseBean>() {
            @Override
            public BaseBean apply(String s) throws Exception {
                return mGson.fromJson(s,BaseBean.class);
            }
        });
    }

    @Override
    public Observable<ResQdetailBean> publishQuestion(String url, String id) {
        HashMap<String,String> params=new HashMap();
        params.put("questid",id);
        return mRepositoryManager.obtainRetrofitService(BusinessService.class).getMethodData(url,params).map(new Function<String, ResQdetailBean>() {
            @Override
            public ResQdetailBean apply(String s) throws Exception {
                return mGson.fromJson(s,ResQdetailBean.class);
            }
        });
    }
}